package com.yocard.yxyd.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.webkit.ValueCallback;
import android.widget.Button;
import android.widget.TextView;

import com.yocard.yxyd.utils.util.ActivityUtils;
import com.yocard.yxyd.R;

/**
 * Created on 2019/4/18.
 */
public class UpdateDialog extends Dialog {

    private String desc;
    private boolean isMust;
    TextView tvDesc;
    Button btnNext;
    private ValueCallback<String> callback;
    protected TextView tvExit;

    public UpdateDialog(@NonNull Context context,String desc,boolean isMust) {
        super(context, R.style.MyDialogStyle);
        this.desc = desc;
        this.isMust = isMust;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_update);
        setCancelable(!isMust);
        setCanceledOnTouchOutside(!isMust);
        tvDesc = findViewById(R.id.tv_desc);
        btnNext = findViewById(R.id.btn_next);
        tvExit = findViewById(R.id.tv_exit);
        tvDesc.setText(desc);
        btnNext.setOnClickListener(v -> {
            if(callback != null){
                dismiss();
                callback.onReceiveValue(null);
            }
        });

        if(isMust){
            tvExit.setVisibility(View.VISIBLE);
            tvExit.setOnClickListener(v -> {
                dismiss();
                ActivityUtils.finishAllActivities();
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
            });
        }
    }

    public void setUpdateListener(ValueCallback<String> callback){
        this.callback = callback;
    }
}
